package com.shihan.rabbitmq.blockingqueuedemo.BlockingQueueMQ;

import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author shihan
 * @Date 2020/12/7 16:38
 * @Version 1.0
 */
public class MyProducer implements Runnable {

    private final BlockingQueue<MyMessageObj> blockingQueue;

    public MyProducer(BlockingQueue<MyMessageObj> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        int count = 0;
        //生产者，不停地往队列中生产物品
        while (true){
            MyMessageObj myMessageObj = new MyMessageObj(UUID.randomUUID().toString(),"msg");
            try {
                blockingQueue.put(myMessageObj);
                count++;
                System.out.println("生产者：" + myMessageObj);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //校验是否阻塞，可以将下面这段return注释掉
            if(count>=10){
                return;
            }
        }
    }
}
